package com.mastercard.labs

import org.springframework.dao.DataIntegrityViolationException

class UserController {
	
	def userService
	
    static allowedMethods = [save: "POST", delete: "POST"]

    def index() {
        redirect(action: "search", params: params)
    }

	def search(){
		if(params.prevAction == 'dashboard'){
			redirect(controller: "dashboard")
		}else if(params.search == "Search"){
			perform()
		}else{
			if(null != params.searchCondition && !"".equals(params.searchCondition)){
				String[] str = params.searchCondition.split("#")
				params.searchUsername = str[0]
				params.searchNric = str[1]
				params.searchName = str[2]
				params.searchStatus = str[3]
				
				perform()
			}
		}
	}

	private def perform(){
		if(!params.searchUsername){
			params.searchUsername = ""
		}
		if(!params.searchNric){
			params.searchNric = ""
		}
		if(!params.searchName){
			params.searchName = ""
		}
		if(!params.searchStatus){
			params.searchStatus = ""
		}
		
		def userInstanceList = userService.searchUserByCondition(params.searchUsername.toUpperCase(), params.searchNric.toUpperCase(), params.searchName, params.searchStatus)
		def searchCondition = params.searchUsername + "#" + params.searchNric + "#" + params.searchName + "#" + params.searchStatus + "#NA#"
		
		[userInstanceList: userInstanceList,
		 searchCondition: searchCondition,
		 searchUsername: params.searchUsername,
		 searchNric: params.searchNric,
		 searchName: params.searchName,
		 searchStatus: params.searchStatus
		]
	}
		
    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [userInstanceList: User.list(params), userInstanceTotal: User.count()]
    }

    def create() {
        [userInstance: new User(params)]
    }
	
	def save() {
		def userInstance
		
		if(params.id){
			userInstance = userService.getUserById(params.id)
			if (!userInstance) {
				flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.name])
				redirect(action: "search", model: [searchCondition: params.searchCondition])
				return
			}
	
			if (params.version) {
				def version = params.version.toLong()
				if (userInstance.version > version) {
					userInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
							  [message(code: 'user.label', default: 'User')] as Object[],
							  "Another user has updated this User while you were editing")
					render(view: "update", model: [userInstance: userInstance, searchCondition: params.searchCondition])
					return
				}
			}
		}

		userInstance.status = User.Status.valueOf(params.status)
		userService.saveUser(userInstance)

		flash.message = message(code: 'default.updated.message', args: [message(code: 'user.label', default: 'User'), userInstance.name])
		redirect(action: "search",  params: [searchCondition: params.searchCondition])
	}
/*
    def save() {
        def userInstance = new User(params)
        if (!userInstance.save(flush: true)) {
            render(view: "create", model: [userInstance: userInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])
        redirect(action: "show", id: userInstance.id)
    }
*/
    def show() {
        def userInstance = User.get(params.id)
        if (!userInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])
            redirect(action: "list")
            return
        }

        [userInstance: userInstance]
    }

    def edit() {
        def userInstance = User.get(params.id)
        if (!userInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])
            redirect(action: "list")
            return
        }

        [userInstance: userInstance]
    }

	def update() {
		def userInstance = userService.getUserById(params.id)
		
		if (!userInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])
			redirect(action: "search")
			return
		}
		
		def statList = new ArrayList()
		statList.add(User.Status.ACTIVE)
		statList.add(User.Status.INACTIVE)
		
		[userInstance: userInstance, searchCondition: params.searchCondition, statList: statList, prevAction: params.prevAction]
	}
/*	
    def update() {
        def userInstance = User.get(params.id)
        if (!userInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (userInstance.version > version) {
                userInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'user.label', default: 'User')] as Object[],
                          "Another user has updated this User while you were editing")
                render(view: "edit", model: [userInstance: userInstance])
                return
            }
        }

        userInstance.properties = params

        if (!userInstance.save(flush: true)) {
            render(view: "edit", model: [userInstance: userInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])
        redirect(action: "show", id: userInstance.id)
    }
*/
    def delete() {
        def userInstance = User.get(params.id)
        if (!userInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])
            redirect(action: "list")
            return
        }

        try {
            userInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'user.label', default: 'User'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'user.label', default: 'User'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
